package logic

import (
	"context"
	"wxapi/internal/services"

	"wxapi/internal/svc"
	"wxapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetStoreListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetStoreListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStoreListLogic {
	return &GetStoreListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetStoreListLogic) GetStoreList(req *types.GetStoreListReq) (resp *types.GetStoreListResp, err error) {
	stores, err := services.GetStoreList(l.ctx, l.svcCtx, req.Lat, req.Lon, req.Province, req.City, req.Area, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}

	resp = &types.GetStoreListResp{
		Store: make([]types.Store, 0, len(stores)),
	}
	for _, store := range stores {
		resp.Store = append(resp.Store, types.Store{
			ID:        store.ID,
			Name:      store.StoreName,
			Avatar:    store.StoreAvatar,
			Province:  store.Province,
			City:      store.City,
			Area:      store.Area,
			Address:   store.StoreAddress,
			State:     store.State,
			OpenTime:  store.StoreOpenTime.Format("H:i"),
			CloseTime: store.StoreCloseTime.Format("H:i"),
			Distance:  services.GetDistance(store.StoreLocationLat, store.StoreLocationLon, req.Lat, req.Lon),
			Phone:     store.StorePhone,
		})
	}

	return
}
